/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hoja6;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
/**
 * 
 * @author  Josue Barrientos - 09049
 */
public class Hoja6 {
        static String txt_code;
        static Map<Character, Integer> letras;
        static Map<String, Character> invertido;
        static String txt_decode;
        static String txt_gen="";
        static String code_gen;
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        //HuffmanCoder form = new HuffmanCoder();
        //form.setVisible(true);  
        
       
        txt_code="abaccda";
        letras= new HashMap(txt_code.length()/2);  
        
        for(char c : txt_code.toCharArray()) {
            if (letras.containsKey(c)==false){
                letras.put(c, 1);
            }
            else{
                letras.put(c,  letras.get(c)+1);
            }
        } 
        invertido= new HashMap(txt_code.length()/2);
        invertido.put("0", 'a');
        invertido.put("110", 'b');
        invertido.put("10", 'c');
        invertido.put("111", 'd');
        
        
        
        Iterator cualquier=letras.keySet().iterator();
        while (cualquier.hasNext()){
            char s= (char) cualquier.next();
            System.out.println(s);
            System.out.println(letras.get(s));
        }
        
        txt_decode="0110010101110";
        decodificar(txt_decode, "");
        System.out.println(txt_gen);

    }
        
    /**
     *
     * @return
     */
    public static void decodificar(String original, String temp){
        temp=temp+original.substring(0, 1);
        original=original.substring(1);
        if (invertido.containsKey(temp)){
            txt_gen=txt_gen+invertido.get(temp);
            temp="";
        }
        if (original.equals("")==false){
            decodificar(original, temp);
        }

    }
    
    
    private static <V, K> Map<V, K> invertMap(Map<K, V> map) {

    Map<V, K> inv = new HashMap<>();

    for (Entry<K, V> entry : map.entrySet()) {
            inv.put(entry.getValue(), entry.getKey());
        }

    return inv;
}

    
}